import { uniq, deepClone } from "../../plugins/utils/likeLodash";
const state = {
  // storeGrapg: {},
  newEdgeOfG6: {
    edgeId: null, // g6Id,
    sourceId: null, // ontologyId,
    targetId: null,
  },
  projectMsg: {}, //导航栏上选中的图谱信息
  projectId: "",
  hideRight: true, // 右侧面板默认隐藏
  hideLeft: false, // 左侧面板默认展开
  hasGraph: true, // 判断是否有图谱
  dataList: [], // 已添加部分的渲染数据
  oldDataList: [], // 当前图谱对应的分析对象
  graphData: {}, // 分析接口返回的图谱数据
  nodeConfig: [
    {
      attribute: "ds",
      radius: "30",
      color: "#F200FF",
    },
    {
      attribute: "dfds",
      radius: "50",
      color: "#002FFF",
    },
  ], // 节点的配置数据
  edgeConfig: [], // 连线的配置数据
  colorArr: [
    "#E9E3A3",
    "#f26522",
    "#d2553d",
    "#84bf96",
    "#ffce7b",
    "#33a3dc",
    "#70a19f",
    "#918597",
  ],
  edgeColor: ["#8D99AE", "#247BA0", "#50514F", "#1d1626", "#293047", "#412f1f"],
  beList: [], // 存储的本体列表
  coList: [], // 存储的连线名列表
  // targetNode: [], // 将要改变样式的目标节点数组
  targetNode: {}, // 将要改变样式的目标节点对象
  targetEdge: {}, // 将要改变样式的目标连线对象
  colorPanelList: [], // 左下角颜色面板的数据
  itemDetail: {}, // 当前点击的节点详情
  currentNodeId: "", // 当前点击的节点 id
  graphWidth: "", // 图谱容器的宽
  graphHeight: "", // 图谱容器的高
  entityTypeList: {
    list: [],
  }, // 筛选的实体类别(本体)
  relationTypeList: {
    list: [
      {
        relationId: "1",
        relationName: "测试机构",
      },
      {
        relationId: "2",
        relationName: "研制方",
      },
      {
        relationId: "3",
        relationName: "研制单位",
      },
    ],
  }, // 筛选的关系类别
  // checked: false, // 默认不全选
  selectedEntityArr: [], // 选中的本体数组
  selectedRelationArr: [], // 选中的关系数组
  allNodesId: [], // 用来保存当前图中所有的节点 id
  kzDataList: [], // 快照列表数据
  drawerFlag: true, // 控制右侧面板是否弹出
  allGraphData: {}, // 用来保存当前图中所有的节点
  graphDatas: {}, // 所有的图节点和边数据
  drawerTypeOfNode: "empty",
  drawerTypeOfEdge: "empty",
  neo4jIdOfNode: null,
  neo4jIdOfEdge: null,
  ontologyId: null,
  currentNode: {},
  currentEdge: {},
  node_config_data: [],
  edge_config_data: [],
  nodeConfigItem: {
    id: -1,
    color: "#57C7E3",
    radius: 30,
    label: "所选节点",
  },
  edgeConfigItem: {
    id: -1,
    label: "所选连边",
    color: "#8D99AE",
    edgeWidth: 2,
    isArrow: true,
  },
};
const mutations = {
  save_graphData(state, payload) {
    state.graphDatas = payload;
  },
  change_drawer_type_of_edge(state, payload) {
    state.drawerTypeOfEdge = payload;
  },
  chang_project(state, payload) {

    state.projectMsg = payload;
  },
  save_nodesId(state, payload) {
    state.allNodesId = payload;
  },
  change_hideRight(state, payload) {
    if (payload) {
      if (!payload.target) {
        state.hideRight = false;
        return;
      }
      state.hideRight = true;
    } else {
      state.hideRight = !state.hideRight;
    }
  },
  change_hideLeft(state, payload) {
    state.hideLeft = !state.hideLeft;
  },
  // 向已添加中添加
  add_dataList(state, payload) {
    state.dataList.push(payload);
  },
  // 从已添加中根据 id 删除
  delete_dataList_byId(state, payload) {
    state.dataList = state.dataList.filter((item) => {
      if (item.id !== payload) return item;
    });
  },
  // 保存当前图谱对应的分析对象数据列表（左侧分析按钮的上一次的值）
  save_oldDataList(state, payload) {
    state.oldDataList = deepClone(state.dataList);
  },
  // 追加后图的数据
  append_graphData(state, payload) {
    state.graphData = payload;
  },
  create_new_edge_of_g6(state, payload) {
    state.newEdgeOfG6 = Object.assign(state.newEdgeOfG6, payload);
  },
  save_beList(state, payload) {
    state.beList.push(payload);
  },
  change_current_node(state, payload) {
    state.currentNode = payload;
  },
  change_current_edge(state, payload) {
    state.currentEdge = payload;
  },
  create_node_config_data(state, payload) {
    state.node_config_data = payload;
    state.node_config_data.forEach((node) => {
      Vue.set(node, "color", node.color);
      Vue.set(node, "radius", 30);
      Vue.set(node, "selected", true);
      // node.radius = 30;
      // node.selected = true;
    });
    state.node_config_data.unshift(state.nodeConfigItem);
  },
  create_edge_config_data(state, payload) {
    state.edge_config_data = payload;
    state.edge_config_data.forEach((edge) => {
      Vue.set(edge, "color", edge.color);
      Vue.set(edge, "edgeWidth", 2);
      Vue.set(edge, "isArrow", true);
      Vue.set(edge, "selected", true);
      // edge.edgeWidth = 1;
      // edge.isArrow = true;
      // edge.selected = true;
    });
    state.edge_config_data.unshift(state.edgeConfigItem);
  },
  change_node_config_data(state, payload) {
    let { nodes, color, radius } = payload;
    if (typeof nodes !== "object") {
      throw Error("请传数组或者对象类型参数");
    }
    let changeTargets = nodes;
    if (!Array.isArray(nodes)) {
      changeTargets = [nodes];
    }

    changeTargets.forEach((item) => {
      let currentNode = state.node_config_data.find((node) => {
        return node.id === item.id;
      });
      if (currentNode) {
        console.log("color", color);
        console.log("radius", radius);
        let _color = color ? color : currentNode.color;
        let _radius = radius ? radius : currentNode.radius;
        Vue.set(currentNode, "color", _color);
        Vue.set(currentNode, "radius", _radius);
      }else{
        Vue.set(nodes, "color", color);
      }
    });
  },
  change_edge_config_data(state, payload) {
    let { edges, color, edgeWidth, isArrow, type } = payload;
    if (typeof edges !== "object") {
      throw Error("请传数组或者对象类型参数");
    }
    let changeTargets = edges;
    if (!Array.isArray(edges)) {
      changeTargets = [edges];
    }
    changeTargets.forEach((item) => {
      let currentEdge = state.edge_config_data.find((edge) => {
        return edge.id === item.id;
      });
      if (currentEdge) {
        let _color = color ? color : currentEdge.color;
        let _edgeWidth = edgeWidth ? edgeWidth : currentEdge.edgeWidth;
        let _isArrow = type === "isArrow" ? isArrow : currentEdge.isArrow;
        Vue.set(currentEdge, "color", _color);
        Vue.set(currentEdge, "edgeWidth", _edgeWidth);
        Vue.set(currentEdge, "isArrow", _isArrow);
      }
    });
  },
  // 保存分析接口返回的图谱数据
  save_graphData(state, payload) {
    const colorArr = [
      "#E9E3A3",
      "#f26522",
      "#d2553d",
      "#84bf96",
      "#ffce7b",
      "#33a3dc",
      "#70a19f",
      "#918597",
    ];
    state.graphData = payload;
    let { nodes, edges } = state.graphData;

    for (let i = 0; i < nodes.length; i++) {
      console.log("ooooo", Object.keys(nodes[i]));
      // state.nodeConfig.push({
      //   attribute: nodes[i].ontology_name,
      //   color: colorArr[parseInt(Math.random() * colorArr.length)],
      //   // radius: nodes[i].size && nodes[i].size / 2,
      //   radius: 15,
      // });
    }
    // state.nodeConfig = uniq(state.nodeConfig, "attribute");
    // state.colorPanelList = state.nodeConfig;
    // 对相同关系的连线去重

    // for (let i = 0; i < edges.length; i++) {
    //   state.edgeConfig.push({
    //     attribute: edges[i].name,
    //     color: colorArr[parseInt(Math.random() * colorArr.length)],
    //     edgeWidth: edges[i].style && edges[i].style.lineWidth,
    //     selected: true,
    //   });
    // }
    state.edgeConfig = uniq(state.edgeConfig, "attribute");

    console.log("nodeConfig", state.nodeConfig);
    console.log("edgeConfig", state.edgeConfig);
  },

  change_hasGraph(state, payload) {
    state.hasGraph = payload;
  },

  save_beList(state, payload) {
    state.beList.push(payload);
  },

  save_coList(state, payload) {
    state.coList.push(payload);
  },

  clear_pz(state, payload) {
    state.nodeConfig = [];
    state.edgeConfig = [];
    state.colorPanelList = [];
  },

  // 清空画布中保存在 store 中的数据
  // clear_graphData(state, payload) {},

  change_nodeCfg(state, payload) {
    console.log("Object.keys(payload)", Object.keys(payload));
    state.nodeConfig.push({
      attribute: payload.ontology_name,
      color: state.colorArr[state.beList.indexOf(payload.ontology_name) % 8],
      radius: 30,
      // radius: payload.size,
    });
    state.nodeConfig = uniq(state.nodeConfig, "attribute");
    state.colorPanelList = state.nodeConfig;
  },

  change_targetNode(state, payload) {
    // let { nodes, edges } = state.graphData;
    state.targetNode = payload;
    console.log("state.targetNode", state.targetNode);
    // state.targetNode.push(payload);
  },

  change_edgeCfg(state, payload) {
    console.log("Object.keys(payload)", Object.keys(payload));
    state.edgeConfig.push({
      attribute: payload.name,
      color: state.edgeColor[state.coList.indexOf(payload.name) % 6],
      edgeWidth: 2,
      selected: true,
    });
    state.edgeConfig = uniq(state.edgeConfig, "attribute");
  },

  change_targetEdge(state, payload) {
    state.targetEdge = payload;
    console.log("state.targetEdge", state.targetEdge);
  },

  change_needInitGraph(state, payload) {
    // state.needInitGraph = true;
  },
  change_graph_size(state, payload) {
    state.graphWidth = payload.width;
    state.graphHeight = payload.height;
  },
  change_hasGraph(state, payload) {
    state.hasGraph = payload;
  },
  change_detail(state, payload) {
    state.itemDetail = payload;
    // console.log("payloadNode", payload);
  },
  change_detail_page(state, payload) {
    state.itemDetail.propertyList = payload;
    // console.log("payloadNode", payload);
  },
  save_caurrentNodeId(state, payload) {
    state.currentNodeId = payload;
  },
  change_entityTypeList(state, payload) {
    const { list } = payload;
    list.forEach((item) => {
      item.selected = false;
    });
    state.entityTypeList = { list, ...payload };
  },
  change_selectedEntityArr(state, payload) {
    if (payload.type === 1) {
      if (payload.change == undefined) {
        const { list } = state.entityTypeList;
        list.forEach((item) => {
          state.selectedEntityArr.push(item.ontologyName);
        });
      } else {
        if (payload.change == "add") {
          state.selectedEntityArr.push(payload.text);
        } else {
          state.selectedEntityArr = state.selectedEntityArr.filter((val) => {
            return val != payload.text;
          });
        }
      }
    } else {
      if (payload.change == undefined) {
        state.selectedEntityArr = [];
      } else {
        if (payload.change == "add") {
          state.selectedEntityArr.push(payload.text);
        } else {
          state.selectedEntityArr = state.selectedEntityArr.filter((val) => {
            return val != payload.text;
          });
        }
      }
    }
    console.log("ontologyName", state.selectedEntityArr);
  },
  // change_entitySelected(state, payload) {
  //   const { list } = state.entityTypeList;
  //   list.forEach((item) => {
  //     if (payload.type === "all") {
  //       item.selected = true;
  //     } else if (payload.type === "none") {
  //       item.selected = false;
  //     }
  //   });
  //   console.log("change_entitySelected", state.entityTypeList);
  // },
  change_relationTypeList(state, payload) {
    const { list } = payload;
    list.forEach((item) => {
      item.selected = false;
    });
    state.relationTypeList = { list, ...payload };
    // console.log("state2", state.relationTypeList);
  },
  change_selectedRelationArr(state, payload) {
    if (payload.type === 1) {
      if (payload.change == undefined) {
        const { list } = state.relationTypeList;
        list.forEach((item) => {
          state.selectedRelationArr.push(item.relationName);
        });
      } else {
        if (payload.change == "add") {
          state.selectedRelationArr.push(payload.text);
        } else {
          state.selectedRelationArr = state.selectedRelationArr.filter(
            (val) => {
              return val != payload.text;
            }
          );
        }
      }
    } else {
      if (payload.change == undefined) {
        state.selectedRelationArr = [];
      } else {
        if (payload.change == "add") {
          state.selectedRelationArr.push(payload.text);
        } else {
          state.selectedRelationArr = state.selectedRelationArr.filter(
            (val) => {
              return val != payload.text;
            }
          );
        }
      }
    }
    console.log("relationName", state.selectedRelationArr);
  },
  change_projectId(state, payload) {
    state.projectId = payload;
  },
  change_kzDataList(state, payload) {
    state.kzDataList = payload;
    console.log("tate.kzDataList", state.kzDataList);
  },
};
const actions = {};
export default {
  state,
  mutations,
  actions,
};
